package com.zengqingfa.juc.n4;

import lombok.extern.slf4j.Slf4j;

/**
 * @fileName: Test1
 * @author: zengqf3
 * @date: 2021-6-9 8:36
 * @description:
 */
@Slf4j(topic = "c.Test1")
public class TestSynchronizedStaticMethod {


    public static void main(String[] args) throws InterruptedException {
        Room2 room = new Room2();
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 5000; i++) {
                room.increment();
            }
        }, "t1");

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 5000; i++) {
                room.decrement();
            }
        }, "t1");
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        //08:51:12:994 [main] c.Test1 - count的值：0
        log.debug("count的值：{}", room.get());
    }


}

class Room2 {
    static int value = 0;

    public synchronized static void increment() {
        value++;
    }

    public synchronized static void decrement() {
        value--;
    }

    public synchronized static int get() {
        return value;
    }
}